package ig.metrica.aplicacion;


import java.sql.SQLException;
import java.lang.Exception;
import ig.da.abs.AbstractBasicDao;
import ig.metrica.aplicacion.AplicacionDTO;

/**
 * GenJ 2011
 * @author usuario
 *
 * Is an Class for table DB for DAO
 */
public class AplicacionDAO extends AbstractBasicDao{

	// -----------------------------------------------------------------
	// Constants
	// -----------------------------------------------------------------

	// -----------------------------------------------------------------
	// Attributes
	// -----------------------------------------------------------------
    private java.sql.Connection connection = null;

	// -----------------------------------------------------------------
	// Constructors
	// -----------------------------------------------------------------
    public AplicacionDAO ()  {

    }

	// -----------------------------------------------------------------
	// All Methods
	// -----------------------------------------------------------------

                                

    /**
     * Method for create an record of table of table
     * @return : the pk of table
     * @param dto
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public  Integer  create(AplicacionDTO dto) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;

        try{
            String QUERY = "INSERT INTO APLICACION ( IDAPLICACION, NOMBRE, VERSION, FECHACTUALIZACION ) VALUES ( ?,  ?,  ?,  ? )";
            if(dto.getPk()==null){
                stat = connection.createStatement();
                res = stat.executeQuery("SELECT MAX(IDAPLICACION) FROM APLICACION");
                if(res.next()){
                    Integer pk = res.getInt (1);
                    dto.setPk(pk !=null ? pk + 1 : 1 );
                }
            }
            prep = connection.prepareStatement(QUERY);
            prep.setInt (1,dto.getIdaplicacion());
            prep.setString (2,dto.getNombre());
            prep.setString (3,dto.getVersion());
            prep.setDate (4,dto.getFechactualizacion());
            prep.executeUpdate();
        } catch (SQLException ex) {
            dto.setPk( null );
            throw ex;
        } catch (Exception ex) {
            dto.setPk( null );
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return dto.getPk();
    }


    /**
     * Update by Pk
     * @param dto
     * @return teh number record updates
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int update (AplicacionDTO dto) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        try{
            String QUERY = "UPDATE APLICACION SET NOMBRE = ?, VERSION = ?, FECHACTUALIZACION = ? WHERE IDAPLICACION = ?";
            prep = connection.prepareStatement(QUERY);
            prep.setString (1,dto.getNombre());
            prep.setString (2,dto.getVersion());
            prep.setDate (3,dto.getFechactualizacion());

            prep.setInt (4,dto.getIdaplicacion());
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Delete by Pk
     * @param pk
     * @return the number record delete
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int delete (AplicacionDTO dto ) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();

        try{
            String QUERY = "DELETE FROM APLICACION WHERE IDAPLICACION = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,dto .getIdaplicacion());
            
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Find by Pk
     * @param pk
     * @return DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public AplicacionDTO findByPk (AplicacionDTO dto ) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        AplicacionDTO aplicacion = null;
        try{
            String QUERY = "SELECT IDAPLICACION, NOMBRE, VERSION, FECHACTUALIZACION FROM APLICACION WHERE IDAPLICACION = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,dto .getIdaplicacion());
            res = prep.executeQuery();
            if (res.next()) {
                aplicacion = new AplicacionDTO(  res.getInt ( 1 ), res.getString ( 2 ), res.getString ( 3 ), res.getDate ( 4 ));
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,res,connection);
        }
        return aplicacion;
    }

    /**
     * Find by All
     * @return the list DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public java.util.List<AplicacionDTO> findAll() throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<AplicacionDTO> list=new java.util.ArrayList<AplicacionDTO>();

        try{
            String QUERY = "SELECT IDAPLICACION, NOMBRE, VERSION, FECHACTUALIZACION FROM APLICACION ";
            prep = connection.prepareStatement( QUERY );
            res = prep.executeQuery();

            while (res.next()) {
                list.add(new AplicacionDTO(  res.getInt ( 1 ), res.getString ( 2 ), res.getString ( 3 ), res.getDate ( 4 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }


    public java.util.List< Integer > createAll(java.util.List<AplicacionDTO> dtos) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;
        java.util.List< Integer > list=new java.util.ArrayList< Integer >();
        try{
            String QUERY = "INSERT INTO APLICACION ( IDAPLICACION, NOMBRE, VERSION, FECHACTUALIZACION ) VALUES ( ?,  ?,  ?,  ? )";
            for(AplicacionDTO dto : dtos ){
                if(dto.getPk()==null){
                    stat = connection.createStatement();
                    res = stat.executeQuery("SELECT MAX(IDAPLICACION) FROM APLICACION");
                    if(res.next()){
                        Integer pk = res.getInt (1);
                        dto.setPk(pk !=null ? pk + 1 : 1 );
                    }
                }
                prep = connection.prepareStatement(QUERY);
                prep.setInt (1,dto.getIdaplicacion());
                prep.setString (2,dto.getNombre());
                prep.setString (3,dto.getVersion());
                prep.setDate (4,dto.getFechactualizacion());
                prep.executeUpdate();
                list.add(dto.getPk());
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return list;
    }






    /**
     * Open the connection
     * @return
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    private java.sql.Connection open() throws ClassNotFoundException, java.sql.SQLException,Exception {
        return getConnection("METRICADW");
    }



}
